package edu.neu.cloud_factory.controller;


import com.alibaba.fastjson.JSONObject;
import edu.neu.cloud_factory.entity.po.Order;
import edu.neu.cloud_factory.entity.po.Product;
import edu.neu.cloud_factory.entity.po.ProductType;
import edu.neu.cloud_factory.entity.vo.ProductVo;
import edu.neu.cloud_factory.service.OrderService;
import edu.neu.cloud_factory.service.ProductService;
import edu.neu.cloud_factory.service.ProductTypeService;
import edu.neu.cloud_factory.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 产品 前端控制器
 * </p>
 *
 * @author halozhy
 * @since 2021-07-15
 */
@RestController
@RequestMapping("/api/prod")
public class ProductController {
    @Autowired
    ProductTypeService productTypeService;

    @Autowired
    ProductService productService;

    @Autowired
    OrderService orderService;

    @GetMapping("/list_all")
    public List<ProductVo> list() {
        List<ProductType> productTypeList = productTypeService.list();

        List<Product> productList = productService.list();
        List<ProductVo> productVoList = new ArrayList<>();
        productList.forEach(product -> {
            ProductVo productVo = new ProductVo();
            productVo.setId(product.getPId());
            productVo.setName(product.getPName());
            productVo.setSpec(product.getPSpec());
            productVo.setDescribe(product.getPDescribe());
            productVo.setType_id(product.getPTypeId());
            for (ProductType pt :
                    productTypeList) {
                if (pt.getPtId().equals(product.getPTypeId())) {
                    productVo.setType_name(pt.getPtName());
                    break;
                }
            }
            productVoList.add(productVo);
        });
        return productVoList;
    }

    @PostMapping("/list")
    public JSONObject list(@RequestBody String CONTENT) {
        JSONObject j = JSONObject.parseObject(CONTENT);
        return productService.listProd(
                j.getInteger("page"),
                j.getInteger("limit"),
                j.getString("id"),
                j.getString("name"),
                j.getString("describe")
        );
    }

    @PostMapping("/add")
    public int add(@RequestBody String CONTENT) {
        JSONObject j = JSONObject.parseObject(CONTENT);
        Product product = new Product();
        if (!Utils.checkEmpty(j, Arrays.asList("name", "type_id"))) {
            return -2; // 存在空字段
        }
        product.setPName(j.getString("name"));
        product.setPSpec(j.getString("spec"));
        product.setPDescribe(j.getString("describe"));
        long typeId = j.getLong("type_id");
        if (productTypeService.getById(typeId) != null) {
            product.setPTypeId(j.getLong("type_id"));
            productService.save(product);
            return 0;
        } else {
            return -1; // 产品类型id错误
        }
    }

    @PostMapping("/update")
    public int update(@RequestBody String CONTENT) {
        JSONObject j = JSONObject.parseObject(CONTENT);
        if (!Utils.checkEmpty(j, Arrays.asList("id", "name", "type_id"))) {
            return -2; // 存在空字段
        }
        Product product = productService.getById(j.getInteger("id"));
        if (product == null) {
            return -1; // 无此产品
        }
        product.setPId(j.getLong("id"));
        product.setPName(j.getString("name"));
        product.setPSpec(j.getString("spec"));
        product.setPDescribe(j.getString("describe"));
        long typeId = j.getLong("type_id");
        if (productTypeService.getById(typeId) == null) {
            return -3; // 产品类型id错误
        }
        product.setPTypeId(j.getLong("type_id"));

        List<Order> orderList = orderService.selectOrderByProductId(product.getPId());
        boolean flag = false;
        for (Order o :
                orderList) {
            if (o.getOState().equals("已生产")) {
                flag = true;
            }
        }
        if (flag) {
            // 当产品正在生产中时，不允许修改
            return -4;
        } else {
            productService.updateById(product);
            return 0;
        }
    }

    @GetMapping("/delete")
    public int delete(int pId) {
        Product product = productService.getById(pId);
        if (product == null) {
            return -2; // 无此产品
        }
        List<Order> orderList = orderService.selectOrderByProductId(product.getPId());
        if (orderList.size() != 0) {
            return -1; // 无法删除，此产品已经被已有订单使用
        }

        productService.removeById(product);
        return 0;
    }

}

